﻿package net.wangit.view.unit.navigator;

import net.wangit.support.PageNavigator;

public class TextPageNavigator implements PageNavigator {

	public TextPageNavigator() {
	}

	public String getHtml(String domain, String unitId, int dataSize,
			int maxSize, int listSize, int pageNumber, int maxPageNumber) {
		StringBuffer html = new StringBuffer();
		html.append("<span class=\"").append("NT").append("\">第").append(
				pageNumber).append("页/共").append(maxPageNumber).append(
				"页&nbsp;&nbsp;显示").append(dataSize).append("条/").append("共")
				.append(maxSize).append("条</span>");
		html.append("&nbsp;&nbsp;");
		setFirst(html, pageNumber);
		html.append("&nbsp;");
		setLast(html, pageNumber);
		html.append("&nbsp;");
		setNext(html, pageNumber, maxPageNumber);
		html.append("&nbsp;");
		setEnd(html, pageNumber, maxPageNumber);
		html.append("&nbsp;");
		html.append("<span class=\"").append("NC").append(
				"\"><input type=\"hidden\" name=\"").append(unitId)
				.append(".p").append("\"/><input class=\"").append("NN")
				.append("\"/><input class=\"").append("NG").append(
						"\" type=\"button\" value=\"go\" onclick=\"_go(")
				.append(pageNumber).append(",").append(maxPageNumber).append(
						")\"/></span>");
		return html.toString();
	}

	private void setFirst(StringBuffer html, int pageNumber) {
		if (pageNumber > 1)
			setEnabledText(html, 1, "首页");
		else
			setDisabledText(html, "首页");
	}

	private void setLast(StringBuffer html, int pageNumber) {
		if (pageNumber > 1)
			setEnabledText(html, pageNumber - 1 >= 1 ? pageNumber - 1 : 1,
					"上一页");
		else
			setDisabledText(html, "上一页");
	}

	private void setNext(StringBuffer html, int pageNumber, int maxPageNumber) {
		if (pageNumber < maxPageNumber)
			setEnabledText(html,
					pageNumber + 1 <= maxPageNumber ? pageNumber + 1
							: maxPageNumber, "下一页");
		else
			setDisabledText(html, "下一页");
	}

	private void setEnd(StringBuffer html, int pageNumber, int maxPageNumber) {
		if (pageNumber < maxPageNumber)
			setEnabledText(html, maxPageNumber, "尾页");
		else
			setDisabledText(html, "尾页");
	}

	private void setEnabledText(StringBuffer html, int pageNumber, String text) {
		html.append("<span class=\"").append("NE").append("\" onclick=\"_to(")
				.append(pageNumber).append(")\">").append(text).append(
						"</span>");
	}

	private void setDisabledText(StringBuffer html, String text) {
		html.append("<span class=\"").append("ND").append("\">").append(text)
				.append("</span>");
	}
}
